#include "bsp_spi.h"

void SPI_Config(void)
{
	SPI_InitTypeDef SPI_InitStructure;
	GPIO_InitTypeDef GPIO_InitStructure;

	FLASH_SPI_APBxClock_FUN(FLASH_SPI_CLK, ENABLE);

	FLASH_SPI_CS_AHBClock_FUN (FLASH_SPI_CS_CLK|FLASH_SPI_SCK_CLK|
	FLASH_SPI_MOSI_CLK, ENABLE);
//	FLASH_SPI_MISO_CLK|FLASH_SPI_MOSI_CLK, ENABLE);

	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
	GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_NOPULL;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_Level_3;
	
	GPIO_InitStructure.GPIO_Pin = FLASH_SPI_CS_PIN;
	GPIO_Init(FLASH_SPI_CS_PORT, &GPIO_InitStructure);

	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;

	GPIO_InitStructure.GPIO_Pin = FLASH_SPI_SCK_PIN;
	GPIO_Init(FLASH_SPI_SCK_PORT, &GPIO_InitStructure);

//	GPIO_InitStructure.GPIO_Pin = FLASH_SPI_MISO_PIN;
//	GPIO_Init(FLASH_SPI_MISO_PORT, &GPIO_InitStructure);
	
	GPIO_InitStructure.GPIO_Pin = FLASH_SPI_MOSI_PIN;
	GPIO_Init(FLASH_SPI_MOSI_PORT, &GPIO_InitStructure);
	
	/* SPI pin mappings */
//	GPIO_PinAFConfig(FLASH_SPI_MISO_PORT, FLASH_SPI_MISO_PIN, SPI_MISO_AF);
//	GPIO_PinAFConfig(FLASH_SPI_MOSI_PORT, FLASH_SPI_MOSI_PIN, SPI_MOSI_AF);
	GPIO_PinAFConfig(FLASH_SPI_SCK_PORT, FLASH_SPI_SCK_PIN, SPI_SCK_AF);

	FLASH_SPI_CS_HIGH();

	SPI_I2S_DeInit(FLASH_SPIx);
	SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
	SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
	SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;

    SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
	SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
	SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;//SPI_NSS_Soft;//SPI_NSS_Hard;
	SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2;
	SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
//	SPI_InitStructure.SPI_CRCPolynomial = 7;
    SPI_Init(FLASH_SPIx, &SPI_InitStructure);

	/* Initialize the FIFO threshold */
	SPI_RxFIFOThresholdConfig(FLASH_SPIx, SPI_RxFIFOThreshold_QF); 

//	SPI_I2S_DMACmd(SPI1,SPI_I2S_DMAReq_Tx,ENABLE);
	/* enable SPI */
	SPI_Cmd(FLASH_SPIx, ENABLE);
}
//
//0x4001 300C
//(APBPERIPH_BASE + 0x00013000)
//((uint32_t)0x40000000 + 0x00013000 + 0x0C)
#define SPI1_DR_ADDRESS     0x4001300C
//((uint32_t)0x40000000 + 0x00003800 + 0x0C)
#define SPI2_DR_ADDRESS     0x4000380C
uint8_t LCD_GRAM[256][128]=
{
	0
};
void DMA_Config(void)	
{
	DMA_InitTypeDef	DMA_InitStructure;
	/* DMA CONFIG*/

	/* DMA1 clock enable */
	RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);

	/* DMA1 Channel1 Config */
	DMA_DeInit(DMA1_Channel1);

	DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)SPI2_DR_ADDRESS;
	DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)LCD_GRAM;

	DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
	DMA_InitStructure.DMA_BufferSize = sizeof(LCD_GRAM);		//1
	DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
	DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
	DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;	//DMA_PeripheralDataSize_Byte	DMA_PeripheralDataSize_HalfWord
	DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;		//DMA_MemoryDataSize_Byte		DMA_MemoryDataSize_HalfWord
	DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
	DMA_InitStructure.DMA_Priority = DMA_Priority_High;
	DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
	DMA_Init(DMA1_Channel1, &DMA_InitStructure);
	/* DMA1 Channel1 enable */
	DMA_Cmd(DMA1_Channel1, ENABLE);	
}



